/**
 * Created by Richard Radlherr.
 * Date: 04.06.2006 21:59
 */

using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using TagTool.Core;
using System.Collections;

namespace TagTool.Components
{
	public partial class OutputSlotPanel
	{
		ArrayList connections = new ArrayList();

		Brush brush = new SolidBrush(Color.Black);
				
		public OutputSlotPanel(OutputSlot slot)
		{
			InitializeComponent();
			this.Tag = slot;
			this.slotName.Text = slot.Name;
			this.Name = slot.Name;
			UpdateValue();
			slotValue.Enabled = slot.Editable;
			Attach();
		}
		
		public void Attach()
		{
			if (ProjectManager.Instance.UI.ShowUpdate)
			{
				Slot.ValueChanged += new ValueChangedEventHandler(ValueChanged);
			}
		}
		
		public void Detach()
		{
			Slot.ValueChanged -= new ValueChangedEventHandler(ValueChanged);
		}

		protected void OnValueChanged()
		{
			try
			{
				((Slot)Tag).Parse(slotValue.Text);
				((Slot)Tag).Owner.Update(((Slot)Tag));
			}
			catch
			{
				UpdateValue();
			}
		}

		public OutputSlot Slot
		{
			get { return (OutputSlot)Tag; }
		}
		
		private void SlotValueTextChanged(object sender, System.EventArgs e)
		{
			OnValueChanged();
		}
		
		private void ValueChanged(object sender, ValueChangedEventArgs e)
		{
			UpdateValue();
		}
		
		public void UpdateValue()
		{
			if (((Slot)Tag).ValueTypeName == null)
			{
				slotValue.Text = ((Slot)Tag).Value.ToString();
			}
			else
			{
				slotValue.Text = ((Slot)Tag).ValueTypeName;
			}
		}
		
		void OutputSlotPanelDragEnter(object sender, System.Windows.Forms.DragEventArgs e)
		{
			IDataObject o = e.Data;
			if (o.GetDataPresent(typeof(InputSlotPanel)))
			{
				InputSlotPanel p = (InputSlotPanel) o.GetData(typeof(InputSlotPanel));
				if (Slot.Allowed(p.Slot.Value))
				{
					e.Effect = DragDropEffects.Move;
				}
				else
				{
					e.Effect = DragDropEffects.None;
				}
			}
		}
		
		void OutputSlotPanelDragDrop(object sender, System.Windows.Forms.DragEventArgs e)
		{
			IDataObject o = e.Data;
			if (o.GetDataPresent(typeof(InputSlotPanel)))
			{
				InputSlotPanel slot = (InputSlotPanel)o.GetData(typeof(InputSlotPanel));
				slot.UnConnect();
				slot.Connect(this);
				Parent.Parent.Invalidate();
			}
		}
		
		public void AddConnection(InputSlotPanel input)
		{
			connections.Add(input);
		}
		
		public void RemoveConnection(InputSlotPanel input)
		{
			connections.Remove(input);
		}
		
		public void UnConnect()
		{
			InputSlotPanel p;
			int i = 0;
			while (i < connections.Count)
			{
				p = (InputSlotPanel)connections[connections.Count - 1]; 
				if (p.Connection == this)
				{
					p.UnConnect();
				}
				else
				{
					i++;
				}
			}
		}
		
		void OutputSlotPanelMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			if (e.Button == MouseButtons.Right)
			{
				//this.contextMenuStrip.Show(slotName.PointToScreen(e.Location));
			}
			else if (e.Button == MouseButtons.Left)
			{
				DoDragDrop(this, DragDropEffects.All);
			}
		}
	}
}
